<template >
  <div class="configReplyEdit">
    <div class="configReplyEditContent">
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="配置名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="配置值" prop="value">
          <el-input v-model="ruleForm.value" :rows="5" type="textarea"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button v-if="this.$route.query.id == null" type="primary" @click="submitForm('ruleForm')">新增</el-button>
          <el-button v-else type="primary" @click="submitForm('ruleForm')">编辑</el-button>
          <el-button v-if="this.$route.query.id == null" @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  components: {
  },
  data() {
    return {
      ruleForm: {
        name: '',
        title: '',
        value: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '请输入文本', trigger: 'blur' }
        ],
        value: [
          { required: true, message: '请输入配置值（JSON格式）', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    if (this.$route.query.id !== null) {
      this.getConfigDetailById()
    }
  },
  methods: {
    getConfigDetailById() {
      this.$http.get('/Config/get?id=' + this.$route.query.id).then(response => {
        if (response.data.code === 0) {
          this.ruleForm = response.data.data
        }
      }).catch(error => {
        this.$message({
          showClose: true,
          message: '获取配置列表详情信息异常{' + error + '}',
          type: 'error'
        })
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 进行编辑行为
          if (this.$route.query.id && this.ruleForm.id) {
            // 编辑自动回复
            this.$http.post('/Config/update', this.ruleForm).then(response => {
              if (response.data.code === 0) {
                this.$message({
                  showClose: true,
                  message: '编辑配置成功！',
                  type: 'success'
                })
              }
            }).catch(error => {
              this.$message({
                showClose: true,
                message: '编辑配置异常{' + error + '}',
                type: 'error'
              })
            })
          } else { // 进行新增行为
            this.$http.post('/Config/add', { name: this.ruleForm.name, title: this.ruleForm.title, value: this.ruleForm.value }).then(response => {
              if (response.data.code === 0) {
                this.$message({
                  type: 'success',
                  message: '添加配置成功'
                })
              }
            }).catch(errorMsg => {
              this.$message({
                type: 'error',
                message: '添加配置异常{' + errorMsg + '}'
              })
            })
          }
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    removeDomain(item) {
      var index = this.ruleForm.domains.indexOf(item)
      if (index !== -1) {
        this.ruleForm.domains.splice(index, 1)
      }
    },
    addDomain() {
      this.ruleForm.domains.push({
        value: '',
        key: Date.now()
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import 'src/styles/variables.scss';
.configReplyEdit {
   padding: 10px;
  .configReplyEditContent {
    min-width: 1100px;
    padding: 20px;
    background-color: white;
    .regionWrapper {
      display: flex;
      .nowSource {
        width: 240px;
        height: 120px;
        border: 2px dotted #d9dadc;
        text-align: center;
        margin-bottom: 20px;
        .homeIcon {
          width: 30px;
          height: 30px;
          margin-top: 20px;
        }
        p {
          color: $color-primary;
        }
      }
      .addSource {
        margin-left: 10px;
        width: 240px;
        height: 120px;
        border: 2px dotted #d9dadc;
        text-align: center;
        margin-bottom: 20px;
        .homeIcon {
          margin-top: 20px;
          width: 30px;
          height: 30px;
        }
        p {
          color: $color-primary;
        }
      }
    }
  }
}
.el-input {
  width: 90%;
  margin-right: 10px;
}
.el-textarea {
  width: 90%;
}
</style>

